<?php

class VideoController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        session_write_close();
        $model = new Video('search');
        $model->status = Video::STATUS_COMPLETE;
        if (isset($_GET['Video'])) {
            $model->attributes = $_GET['Video'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }

    public function actionPlay()
    {
        if (isset($_GET['id'])) {
            $model = Video::model()->findByPk($_GET['id']);
            $this->render('play', array(
                'model' => $model,
            ));
            return;
        } else {
            $this->redirect($this->defaultAction);
        }
    }

    /**
     * @param Video $data
     * @param $row
     * @param $c
     * @return mixed
     */
    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'operations':
                $params = [];
                $res = '';
                $params[] = Yii::app()->getController()->createUrl("play", array("id" => $data->id,));
                $res .= '<a class="" aria-hidden="true" href="%s">播放</a>';
                return vsprintf($res, $params);
                break;
            case 'status':
                $html = '';
                $color = ($data->status == Video::STATUS_COMPLETE ? ("green") : ($data->status == Video::STATUS_FAILED ? "red" : ""));
                $html .= "<p style=\"color: $color \"> " . Video::getOptions('status')[$data->status] . "</p>";
                return $html;
                break;
            case 'task':
                $task = $data->task;
                if ($task) {
                    $html = $task->name;
                    return $html;
                }
                break;
            case 'time_create':
            case 'time_update':
                return date('Y-m-d H:i:s',$data->time_create);
                break;
            case 'avetar':
                $html = '';
                $html .='<img class="topic-img" src="%s">';
                $html = vsprintf($html, [$data->avetar,]);
                return $html;
                break;
            default:
                return $data->$name;
        }
    }


}